package cn.edu.lingnan.servlet;

import cn.edu.lingnan.pojo.Competition;
import cn.edu.lingnan.pojo.Participation;
import cn.edu.lingnan.service.ICompetitionService;
import cn.edu.lingnan.service.IParticipationService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@WebServlet("/queryAllPart")
public class queryParticipation extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String partName = req.getParameter("partName");
        IParticipationService service = new IParticipationService();
        ICompetitionService service1 = new ICompetitionService();
        List<Competition> competitions = service1.findCompetitionAll();
        List<Participation> list = service.findParticipationAll();
        List<Participation> list2 = service.findParticipationByName(partName);
        //自定义排序，按status为0,1,2升序排序，按分数降序排序
        Collections.sort(list, (p1, p2) -> {
            // 按照 getStatus() 进行排序
            if (p1.getStatus() != p2.getStatus()) {
                // 0 排在最前面
                if (p1.getStatus() == 0) {
                    return -1;
                } else if (p2.getStatus() == 0) {
                    return 1;
                }

                // 2 排在最后面
                if (p1.getStatus() == 2) {
                    return 1;
                } else {
                    return -1;
                }
            }

            // 分数降序排列
            return Float.compare(p2.getScore(), p1.getScore());
        });

// 遍历每个竞赛项目

        for (Competition competition : competitions) {
            String competitionName = competition.getName(); //找出该竞赛被选的记录
            List<Participation> participations = service.findParticipationByNameStatus(competitionName);

            // 根据该竞赛项目的分数对参与记录进行排序
            Collections.sort(participations, Comparator.comparingDouble(Participation::getScore).reversed());

            // 分配排名给该竞赛项目的参与记录
            int rank = 1;
            for (Participation participation : participations) {
                participation.setCompetition_rank(rank);
                service.updateRank(participation.getStudent_id(),participation.getCompetition_name(),rank);//同步更新数据库里的排名
                rank++;
            }
            // 使用session更新该竞赛项目的参与记录
            req.getSession().setAttribute("part_" + competitionName, participations);
        }

            HttpSession session = req.getSession();
            session.setAttribute("competitions",competitions);
            if (partName == null || partName.equals("")) {
                session.setAttribute("allPart", list);// 默认查询所有比赛记录
            } else {
                Collections.sort(list2, Comparator.comparingDouble(Participation::getScore).reversed());
                session.setAttribute("allPart", list2);// 模糊查询单个比赛记录
            }

        resp.sendRedirect(req.getContextPath() + "admin/Participation.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}